package org.lee.springcloud.provider.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Random;

/**
 * @author jackielee
 * @version 1.0
 * @description RetryController
 * @date 2021/3/26
 */
@RestController
@RequestMapping("/retry")
public class RetryController {

  private final Logger logger = LoggerFactory.getLogger(RetryController.class);

  @Value("${server.name}")
  private String serverName;

  @GetMapping("/hello")
  public String hello(String name, HttpServletResponse response) throws InterruptedException {
    Random random = new Random(new Date().getTime());
    int val = random.nextInt(100);
    logger.info(String.format("hello %s,the val:%d before", name, val));
    if(val % 10 == 0 || val % 3 == 0 || val % 2 == 0){ // 增加错误概率
      Thread.sleep(5000);
      response.setStatus(500); // 设置为响应500
      logger.info(String.format("hello %s,the val:%d by awaking.(5s)", name, val));
    }
    logger.info(String.format("hello %s,the val:%d after", name, val));
    return "hello " + name + " this time is ok.(from " + serverName + ")"+val;
  }
}
